package com.isoft.airportsystem.service.validation;

import com.isoft.airportsystem.entity.change.Flight;
import com.isoft.airportsystem.exception.BusinessException;
import com.isoft.airportsystem.mapper.change.FlightchangeMapper;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class FlightChangeValidator {

    private final FlightchangeMapper flightMapper;

    public FlightChangeValidator(FlightchangeMapper flightMapper) {
        this.flightMapper = flightMapper;
    }

    // 添加航班变更验证方法
    public void validateFlightForChange(String flightId, String changeType) {
        Flight flight = flightMapper.selectById(flightId);

        // 如果航班不存在，允许提交申请（可能是新航班）
        if (flight == null) {
            // 允许所有变更类型在航班不存在时提交
            return; // 允许提交
        }

        switch (changeType) {
            case "DELAY":
                validateForDelay(flight);
                break;
            case "CANCEL":
                validateForCancel(flight);
                break;
            case "DIVERT":
                validateForDivert(flight);
                break;
            case "GATE_CHANGE":
                validateForGateChange(flight);
                break;
            case "TIME_ADJUSTMENT":
                validateForTimeAdjustment(flight);
                break;
            case "AIRCRAFT_CHANGE":
                validateForAircraftChange(flight);
                break;
            default:
                throw new BusinessException("未知的变更类型: " + changeType);
        }
    }

    // 添加延误变更验证方法
    private void validateForDelay(Flight flight) {
        // 更新：允许在已延误状态下再次申请延误变更
        List<String> allowedStatus = Arrays.asList("SCHEDULED", "CHECKED_IN", "ON_TIME", "DELAYED");

        if (!allowedStatus.contains(flight.getStatus())) {
            throw new BusinessException("航班状态[" + flight.getStatus() + "]不允许延误变更");
        }
    }

    // 添加取消变更验证方法
    private void validateForCancel(Flight flight) {
        List<String> allowedStatus = Arrays.asList("SCHEDULED", "CHECKED_IN", "ON_TIME");

        if (!allowedStatus.contains(flight.getStatus())) {
            throw new BusinessException("航班状态[" + flight.getStatus() + "]不允许取消变更");
        }
    }

    // 添加备降变更验证方法
    private void validateForDivert(Flight flight) {
        List<String> allowedStatus = Arrays.asList("SCHEDULED", "CHECKED_IN");

        if (!allowedStatus.contains(flight.getStatus())) {
            throw new BusinessException("航班状态[" + flight.getStatus() + "]不允许备降变更");
        }
    }

    // 添加登机口变更验证方法
    private void validateForGateChange(Flight flight) {
        List<String> allowedStatus = Arrays.asList("SCHEDULED", "CHECKED_IN", "ON_TIME");

        if (!allowedStatus.contains(flight.getStatus())) {
            throw new BusinessException("航班状态[" + flight.getStatus() + "]不允许登机口变更");
        }
    }

    // 添加时间调整验证方法
    private void validateForTimeAdjustment(Flight flight) {
        // 时间调整在任何状态下都允许
        // 不需要状态验证
    }

    // 添加飞机变更验证方法
    private void validateForAircraftChange(Flight flight) {
        // 飞机变更在任何状态下都允许
        // 不需要状态验证
    }
}